package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.SportAnalyzeActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.model.ActionsAmountModel;
import com.coollang.smartbra.model.FooterCountModel;
import com.coollang.smartbra.model.GoldAndActionModel;
import com.coollang.smartbra.model.LogInDataBean;
import com.coollang.smartbra.model.MonthBillModel;
import com.coollang.smartbra.utils.Urls;
import com.coollang.smartbra.views.RadarData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @Auther: Timmy
 * @Data: 2017/7/13
 * @Fuction:
 */

public class SportAnalyzeActivityPresenter extends BasePresenter<MonthBillModel> {

    private SportAnalyzeActivity mSportAnalyzeActivity;
    // 获取用户状态
    private int requestMode;
    private Map<String, String> mMap;
    private LogInDataBean mLogInDataBean;

    public SportAnalyzeActivityPresenter(SportAnalyzeActivity sportAnalyzeActivity) {
        super(MonthBillModel.class);
        mSportAnalyzeActivity = sportAnalyzeActivity;
        mLogInDataBean = MyApplication.mLogInDataBean;
    }

    @Override
    public void setRequestInfo() {

    }

    @Override
    protected Map<String, String> getData() {
        return mMap;
    }

    @Override
    protected void parsrData(MonthBillModel model) {

        if (model.getCode() == 200) {
            parsrMonthBill(model.getData());
        }

    }

    /**
     * 获取所有运动的总数
     */
    public void getActionsAmoun() {
        MyHttp.getInstance().getActionsAmount(new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                ActionsAmountModel model = mGson.fromJson(results, ActionsAmountModel.class);
                if (model.getCode() == 200) {
                    parserActionsAmount(model.getData());
                } else {
                    MyHttp.getInstance().showErrorToast(mSportAnalyzeActivity, model.getCode());
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                MyHttp.getInstance().showErrorToast(mSportAnalyzeActivity, Urls.CODE_Error_);
            }
        });
    }

    /**
     * 获取近六个月U币
     */
    public void getMonthBill() {
        requestMode = 1;
        mMap = new HashMap<>(16);
        mMap.put("username", mLogInDataBean.getUsername());
        setBaseOkgo("count/month-bill");
    }

    /**
     * 解析所有运动的总数
     */
    private void parserActionsAmount(List<ActionsAmountModel.DataBean> data) {
        Integer[] actionCode = SportAnalyzeActivity.getActionCode();

        List<RadarData> dataList = new ArrayList<>();
        for (Integer integer : actionCode) {
            for (ActionsAmountModel.DataBean dataBean : data) {
                int code = Integer.parseInt(dataBean.getCode());
                if (integer == code) {
                    String name = dataBean.getName();
                    float percent = dataBean.getPercent();
                    RadarData radarData = new RadarData(name, percent * 10);
                    dataList.add(radarData);
                    break;
                }
            }
        }

        if (dataList.size() > 0) {
            mSportAnalyzeActivity.mRadarView.setDataList(dataList);
        }

        /**

         List<Float> values2 = new ArrayList<>();
         Collections.addAll(values2,
         data.get(3).getPercent() * 10f,
         data.get(5).getPercent() * 10f,
         data.get(7).getPercent() * 10f,
         data.get(2).getPercent() * 10f,
         data.get(1).getPercent() * 10f,
         data.get(0).getPercent() * 10f,
         data.get(4).getPercent() * 10f,
         data.get(5).getPercent() * 10f);
         */
        //  RadarData data2 = new RadarData(values2);
        // data2.setColor(0X8CFFFFFF);
        // mSportAnalyzeActivity.mRadarView.addData(data2);
        // mSportAnalyzeActivity.mRadarView.setLayer(1);
    }

    /**
     * 解析六个月数据
     *
     * @param data
     */
    private void parsrMonthBill(List<MonthBillModel.DataBean> data) {
        String[] monthText = new String[6];
        int[] record = new int[6];

        int size = data.size();
        for (int i = 0; i < size; i++) {
            MonthBillModel.DataBean dataBean = data.get(i);

            String month = dataBean.getMonth();
            monthText[i] = Integer.parseInt(month.substring(month.length() - 2, month.length())) + "月";

            record[i] = dataBean.getNumber();
        }

        mSportAnalyzeActivity.mScoreTrend.setMonthText(monthText);
        mSportAnalyzeActivity.mScoreTrend.setScore(record);
    }


    public GoldAndActionModel queryGoldAndAction() {
        return mRealm.where(GoldAndActionModel.class).findFirst();
    }


    public void getFooterCount() {
        MyHttp.getInstance().getFooterCount(new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                FooterCountModel model = mGson.fromJson(results, FooterCountModel.class);
                int code = model.getCode();
                if (code == 200) {
                    // 数据解析成功
                    FooterCountModel.DataBean data = model.getData();

                    String level = parserFooterData(data.getStrength());

                    String text = "评分为 " + data.getCoin() + " , 在 " + level + " 的队伍中";
                    mSportAnalyzeActivity.setScoreText(text);
                    mSportAnalyzeActivity.setScoreText(data.getCoin(), data.getStrength());

                } else {
                    MyHttp.getInstance().showErrorToast(mSportAnalyzeActivity, code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                MyHttp.getInstance().showErrorToast(mSportAnalyzeActivity, Urls.CODE_Error_);
            }
        });
    }

    public String parserFooterData(int count) {

        String level = "";

        switch (count) {
            case 1:
                level = "训练新手";
                break;
            case 2:
                level = "有些生疏";
                break;
            case 3:
                level = "定期训练";
                break;
            case 4:
                level = "训练达人";
                break;
            default:
                break;
        }
        return level;
    }
}
